<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拖拽</title>
    <style>
        *{
            padding:0;
            margin:0;
        }
        html,body,.box{
            width: 100%;
            height:100%;
            overflow: hidden;
        }
        .box{
            position: relative;
        }

        .shadow{
            width:100px;
            height: 100px;
            background: orange;
            /* border-radius: 50%; */
            position: absolute;
            top:50px;
            left:50px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    
    <div class="shadow"></div>
    <div class="shadow"></div>
    <div class="shadow"></div>
    <div class="shadow"></div>
    <div class="shadow"></div>
    <div class="shadow"></div>
    <div class="shadow"></div>
    <div class="shadow"></div>
    <script>
        // 工厂模式    批量生产 可以同时返回多个 this 对象  
        // 单例模式    单个模式  一次只有一个 this 对象  多个单例组成工厂模式 (混合模式)




        // 元素拖拽
        // 1. 先按下
        // 2. 鼠标滑动  改变对应的位置  left top
        // 3. 鼠标离开   松开  不再移动
        var shadow = document.getElementsByClassName('shadow')[0];
        var clientWidth = document.documentElement.clientWidth || document.body.clientWidth;
        var clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
        var maxLeft = clientWidth - shadow.clientWidth;    // 可以拖拽的 宽度
        var maxTop = clientHeight - shadow.clientHeight;  // 可以拖拽的 高度 

        // dragStart  x,y 这2个变量传递给 dragMove
        // dragMove
        // dragEnd

        // 改写构造函数  单例模式
        function DragFunc(ele){
            var _this = this;
            // 属性 
            this.target = ele;   // this 指向实例对象 
            this.x = 0;
            this.y = 0;

            this.dragStart = function(){
                // this ==> ?
                this.target.onmousedown = function(e){
                    var e= e || window.event;
                    console.log("onmousedown");

                    console.log(this)  // this = this.target 
                    _this.x = e.clientX - this.offsetLeft;
                    _this.y = e.clientY - this.offsetTop;


                    _this.dragMove();
                    _this.dragEnd();
                    // document.onmousemove = function(e){
                    //     var e= e || window.event;
                    //     var left = e.clientX - _this.x;
                    //     var top = e.clientY - _this.y;
                        
                    //     if(left<0) left = 0;
                    //     if(left>maxLeft) left = maxLeft;
                    //     if(top<0) top = 0;
                    //     if(top>maxTop) top = maxTop;

                    //     _this.target.style.top = top+'px'
                    //     _this.target.style.left = left +'px'
                        
                    // }

                    // document.onmouseup = function(){
                    //     document.onmouseup = null
                    //     document.onmousemove = null;
                    // }
                }
            }

            // 方法 
            this.dragMove = function(){
                document.onmousemove = function(e){
                    var e= e || window.event;
                    var left = e.clientX - _this.x;
                    var top = e.clientY - _this.y;
                    
                    if(left<0) left = 0;
                    if(left>maxLeft) left = maxLeft;
                    if(top<0) top = 0;
                    if(top>maxTop) top = maxTop;
                    _this.target.style.top = top+'px'
                    _this.target.style.left = left +'px'
                    
                }
            }

            // 方法 
            this.dragEnd = function(){
                document.onmouseup = function(){
                    document.onmouseup = null
                    document.onmousemove = null;
                }
            }

            this.randColor = function(){
                var colorStr = "0123456789abcdef";
                var color = "#";
                for(var i=0;i<6;i++){
                    var index = Math.floor(Math.random() * colorStr.length );
                    var char = colorStr.charAt(index);
                    console.log(char);
                    color += char;
                }
                this.target.style.background = color;
            }

            this.dragStart()
            this.randColor()
        }

        // var obj = new DragFunc(shadow)
        
        var slist = document.getElementsByClassName("shadow");
        for(var i=0;i<slist.length;i++){
            var sdbox = slist[i];
            // DragFunc(sdbox);
            var obj = new DragFunc(sdbox);  // this = obj
            
        }
       


        // 换成构造函数 
    </script>
</body>
</html>